<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Active_con extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		$this->load->library('PHPexcel');
		$this->load->helper('download');
		$this->load->helper('file');

	}
	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array(
	  		'zones' => $this->active_con_model->get_zones()
	  	));
	  $this->load->view('layouts/footer');
	 }

	public function get_status_ac(){
	 	$stat   = $this->input->post('stat_type');
	 	$zone 	= $this->input->post('cus_zone');

	 $result = $this->active_con_model->get_status($stat,$zone);
		echo json_encode($result);		
	 }


	  public function gen_pdf() {
	  	$stat = 'null';
	  	$stat_l ="";
		if (!empty($_GET['stat_type'])) {
			$stat = $_GET['stat_type'];
				if($stat == 'ACT'){ $stat_l = "Active"; }
				else if($stat == 'INACT'){ $stat_l = "In-Active"; }
				else {$stat_l = "PND";}

		}
		$zones = $_GET['zones'];
		$result = $this->active_con_model->get_status($stat,$zones);
		//var_dump($result);
		$table_data = '';
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		//<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		foreach ($result as $key) {
			$table_data .= '<tr><td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="center">' . $key->acct_num . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="left">' . $key->acct_name . '</td></tr>';
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Customers :" . $stat_l . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
				<tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Zone :" . $zones . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
				<tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Total Count :" . $_GET['total'] . "</h4>
					</td>
					
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
				
			</table><br><br>
			<table cellspacing='0' style='width: 80%; position: relative;' align='center'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=40%; padding: 5px;'>Account Number</td>
					<td style='border-bottom:1px solid; text-align: center;width=40%; padding: 5px;'>Account Name</td>
				</tr>" .
			$table_data
			. "
			</table>
		</page>
		";
		require_once dirname(__FILE__) . '\..\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Customer List');
		$html2pdf->WriteHTML($content);
		$html2pdf->Output('Customer_list' . date("dmY_His") . '.pdf', 'D');
	}

	public function gen_excel() {
		$stat_l ='';
		$stat = 'null';
		$total = 'null';
		if (!empty($_GET['stat_type'])) {
			$stat = $_GET['stat_type'];
				if($stat == 'ACT'){ $stat_l = "Active"; }
				else if($stat == 'INACT'){ $stat_l = "In-Active"; }
				else {$stat_l = "PND";}

		}
		if (!empty($_GET['total'])) {
			$total = $_GET['total'];
		}
		$zones = $_GET['zones'];
		//activate worksheet number 1
		$excel = new PHPExcel();
		$excel->setActiveSheetIndex(0);
		//name the worksheet
		$excel->getActiveSheet()->setTitle('Customer List');
		//set cell A1 content with some text
		$excel->getActiveSheet()->setCellValue('A1', 'Customer : ' . $stat_l . '     Zone : ' . $zones);
		$excel->getActiveSheet()->setCellValue('A3', 'Account Number');
		$excel->getActiveSheet()->setCellValue('B3', 'Account Name');

		$result = $this->active_con_model->get_status($stat,$zones);

		$cellnum = 4;

		foreach ($result as $key) {
			$excel->getActiveSheet()->setCellValue('A' . $cellnum, $key->acct_num);
			$excel->getActiveSheet()->setCellValue('B' . $cellnum, $key->acct_name);
			$cellnum++;
		}
		$excel->getActiveSheet()->setCellValue('A' . $cellnum, 'Total Count');
		$excel->getActiveSheet()->setCellValue('B' . $cellnum, $total);
		//$excel->getActiveSheet()->getStyle('A1:F' . $cellnum = $cellnum - 1)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

		// $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->getStartColor()->setARGB('29bb04');
		//       // Add some data
		//       $excel->getActiveSheet()->getStyle("A1:F1")->getFont()->setBold(true);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->getStartColor()->setARGB('6F6F6F');
		// Add some data
		$excel->getActiveSheet()->getStyle("A3")->getFont()->setBold(true);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
		//change the font size
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);
		//make the font become bold
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$excel->getActiveSheet()->mergeCells('A1:F1');
		//set aligment to center for that merged cell (A1 to D1)
		$excel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		//$excel->getActiveSheet()->getStyle("A2:F2")->getFont()->getColor()->setRGB('FFFFFF');

		$excel->getActiveSheet()->setShowGridlines(false);

		$filename = 'customer_list_ ' . date('Ymd_his') . '.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
		$objWriter->save('php://output');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("assets/tmp/" . $filename);
		//$this->index();
		// $data = ob_get_contents();
		//$tester =  $objWriter->save($filename);
		//force_download($filename,$objWriter->save('php://output')));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */